<template>
  <div class="login-container">
    <div class="background-effect"></div>
    <div class="login-form">
      <div class="welcome-message styled-welcome">欢迎登录</div>
      <el-form ref="form" :model="form" :rules="rules" size="normal">
        <el-form-item prop="username">
          <el-input :prefix-icon="User" v-model="form.username" placeholder="用户名" class="rounded-input" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input :prefix-icon="Hide" v-model="form.password" show-password placeholder="密码" class="rounded-input" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login" class="styled-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { User, Hide } from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  name: "UserLogin",
  components: {
    User,
    Hide
  },
  data() {
    return {
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    };
  },
  methods: {
    login() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          request.post("/user/login", this.form).then(res => {
            if (res.code === '0') {
              this.$message({
                type: "success",
                message: "登录成功"
              });
              sessionStorage.setItem("user", JSON.stringify(res.data));
              this.$router.push("/User");
            } else {
              this.$message({
                type: "error",
                message: res.msg
              });
            }
          });
        }
      });
    }
  }
};
</script>

<style scoped>
:root {
  --primary-color: #007BFF; /* 蓝色 */
  --secondary-color: #ADD8E6; /* 浅蓝色 */
  --text-color: #333; /* 深灰色文字 */
  --background-color: #F0F8FF; /* 浅蓝色背景 */
}

.login-container {
  width: 100%;
  height: 100vh;
  background-color: var(--background-color);
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  position: relative;
}

.background-effect {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #007BFF, #ADD8E6);
  z-index: -1;
  opacity: 0.1; /* 调整透明度以适应浅蓝色背景 */
  animation: gradient 15s infinite;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-form {
  width: 400px;
  margin: 100px auto;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease-in-out;
}

.login-form:hover {
  transform: scale(1.05);
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
}

.welcome-message {
  color: var(--text-color);
  font-size: 30px;
  text-align: center;
  padding: 30px 0;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
  font-weight: bold;
}

.rounded-input .el-input__inner {
  border-radius: 20px;
  background-color: rgba(255, 255, 255, 0.9);
  border: none;
  padding: 10px 20px;
  transition: all 0.3s ease-in-out;
}

.rounded-input .el-input__inner:focus {
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 1);
}

.styled-button {
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  transition-duration: 0.4s;
  cursor: pointer;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}

.styled-button:hover {
  background-color: #0056b3;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.2);
}

.styled-welcome {
  font-size: 32px;
  font-weight: bold;
  color: var(--text-color);
  text-align: center;
  margin-bottom: 20px;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
  font-family: "SimHei", sans-serif;
}
</style>